package br.ufpe.cin.rgms.linhapesquisa.apresentacao;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import br.ufpe.cin.rgms.Facade;
import br.ufpe.cin.rgms.base.RGMSException;
import br.ufpe.cin.rgms.linhapesquisa.modelo.LinhaPesquisa;
import br.ufpe.cin.rgms.util.Properties;

public class AlterarDadosLinhaPesquisaServlet extends HttpServlet {

	private HashMap<String, String> formfields;

	@Override
	public void init() throws ServletException {
		super.init();

		this.formfields = new HashMap<String, String>();
	}

	private void processFormField(FileItem item) {
		// Process a regular form field
		if (item.isFormField()) {
			String name = item.getFieldName();
			String value = item.getString();

			this.formfields.put(name, value);
		}
	}

	public void doPost( HttpServletRequest request, 
			HttpServletResponse response) 
	throws IOException, ServletException {
		// Create a factory for disk-based file items
		FileItemFactory factory = new DiskFileItemFactory();

		// Create a new file upload handler
		ServletFileUpload upload = new ServletFileUpload(factory);

		try {
			List /* FileItem */ items = upload.parseRequest(request);

			// Process the uploaded items
			Iterator iter = items.iterator();

			while (iter.hasNext()) {
				FileItem item = (FileItem) iter.next();

				if (item.isFormField()) {
					processFormField(item);
				} 
			}

			String titulo = this.getFormfields().get("titulo"); 
			String descricaoBasica = this.getFormfields().get("descricaoBasica"); 
			String descricaoDetalhada = this.getFormfields().get("descricaoDetalhada"); 
			String financiadores = this.getFormfields().get("financiadores");
			String linksRelacionados = this.getFormfields().get("linksRelacionados");

			LinhaPesquisa linhapesquisaParaAlterar = Facade.getInstance().getLinhaPesquisa(titulo);

			linhapesquisaParaAlterar.setDescricaoBasica(descricaoBasica);
			linhapesquisaParaAlterar.setDescricaoDetalhada(descricaoDetalhada);
			linhapesquisaParaAlterar.setFinanciadores(financiadores);
			linhapesquisaParaAlterar.setLinksRelacionados(linksRelacionados);

			Facade.getInstance().alterarLinhaPesquisa(linhapesquisaParaAlterar);

			request.setAttribute("linhapesquisastatus", Properties.getProperty(this.getServletContext(),"alteracao_sucesso"));

		} catch (FileUploadException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (RGMSException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("linhapesquisastatus", e.getMessage());
		}

		RequestDispatcher view = request.getRequestDispatcher("linhapesquisastatus.jsp");
		view.forward(request, response);
	}

	public HashMap<String, String> getFormfields() {
		return this.formfields;
	}
}
